#include<iostream>
#include<vector>
#include<sys/time.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
vector<int**>mv;
int www = 50;
void getMatrix()
{
        int temp = 0;
        for(int i = 1;i<=20;i++)
        {
                temp = i * www;
                int**t = new int*[temp];
                for(int p = 0;p < temp;p++)
                {
                        t[p] = new int[temp];
                        for(int q = 0; q < temp ;q++)
                        {
                                t[p][q] = rand()%100 + 1;
                        }
                }
                mv.push_back(t);
        }
}
void cache(int **m,int len)
{
        int *v = new int[len];
        for(int i = 0 ;i < len;i++)
        {
                v[i] = rand()%10 + 1;
        }
        int *sum = new int[len];
        for(int i = 0;i < len; i++)
        {
                sum[i] = 0;
        }
        for(int i = 0;i< len;i++)
        {
                for(int p =0 ;p < len; p++)
                {
                        sum[p] += m[i][p] * v[i];
                }
        }
}
void cal(int **m, int len)
{
        int * v = new int[len];
        for(int i = 0;i < len ;i++)
        {
                v[i] = rand() % 10 + 1;
        }
        int * sum = new int [len];
        for(int i = 0 ;i < len; i++)
        {
                sum[i] = 0;
                for(int p = 0;p < len; p ++)
                {
                        sum[i] += v[p] * m[p][i];
                }
        }
}
int main()
{
        getMatrix();
        struct timeval t1,t2;
        double timeuse;
        int temp = 0;
        for(int i = 1;i <= 20;i++)
        {
                temp = i * www;
                gettimeofday(&t1,NULL);

                cache(mv[i - 1], temp);
                //cal(mv[i - 1], temp);

                gettimeofday(&t2,NULL);
                timeuse = (t2.tv_sec - t1.tv_sec) + (double)(t2.tv_usec - t1.tv_usec)/1000000.0;

                cout<<timeuse<< std::endl;  //输出时间（单位：ｓ）
        }
        //cout<<"pid = "<<getpid()<<endl;
        //getchar();// linux经常使用， 让程序暂停下来
        return 0;
}
